﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using Cn.Edwon.Web.Session;
using Cn.Edwon.Web.Ac;
using Common.Logging;

namespace Cn.Edwon.Web.Support {
    public class WucSupport : System.Web.UI.UserControl,IActorSessOper {
        public static ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        protected override void OnInit(EventArgs e) {
            base.OnInit(e);
            _Ac = ClientHelper.CreateAc();
            _Ac.Actor = Actor;
        }

        private IAuthentication _Ac;
        public IAuthentication Ac {
            get { return _Ac; }
            set { _Ac = value; }
        }


        #region IActorSessOper 成员

        public ISessActor Actor {
            set {
                log.Debug("为actor session赋值");
                Session[ClientHelper.SESS_USER_KEY] = value; }
            get {
                if (Session[ClientHelper.SESS_USER_KEY] == null) {
                    log.Debug("从session获取actor(null)");
                    return null;
                } else {
                    log.Debug("从session获取actor");
                    return (ISessActor)Session[ClientHelper.SESS_USER_KEY];
                }
            }
        }

        public void ClearActor() {
            log.Debug("清除actor session");
            Session[ClientHelper.SESS_USER_KEY] = null;
            Session.Remove(ClientHelper.SESS_USER_KEY);
        }
        /// <summary>
        /// 属主Key
        /// </summary>
        public String OwnerKey {
            get {
                if (this.Actor != null) {
                    return this.Actor.OwnerKey;
                } else {
                    return SupportUtil.OWNER_KEY_DEFAULT;
                }
            }
        }
         public UserPower Power {
            get {
                if (this.Actor != null) {
                    return this.Actor.Power;
                } else {
                    return new UserPower();
                }
            }
        }
        #endregion
    }
}
